import cv2

def capture_video_from_camera(device_path, display_delay=1000):
    # 使用 OpenCV 初始化视频捕获
    cap = cv2.VideoCapture(device_path)

    if not cap.isOpened():
        print("Error: Cannot open camera")
        exit()

    try:
        while True:
            # 读取一帧
            ret, frame = cap.read()

            # 如果正确读取帧，ret为True
            if not ret:
                print("Can't receive frame (stream end?). Exiting ...")
                break

            # 显示结果帧
            cv2.imshow('Frame', frame)

            # 按 'q' 键退出循环
            if cv2.waitKey(display_delay) & 0xFF == ord('q'):
                break

    finally:
        # 释放捕获
        cap.release()
        cv2.destroyAllWindows()

if __name__ == '__main__':
    device_path = '/dev/video0'
    capture_video_from_camera(device_path)